#include "KKApplication.h"
#include "KKComm.h"
#include "KKRenderGdiPlus.h"
#include "KKUIBase.h"
#include "KKWindow.h"

void updateUI(KKWindow *win)
{
    auto ui_root = new KKUIBase(nullptr);
    ui_root->setProperty(KK_CSS_PROPERTY_CONTENT, KKUIPropertyValue{"hello world!"});
    ui_root->setProperty(KK_CSS_PROPERTY_TEXT_ALIGN, KKUIPropertyValue{KK_TEXT_ALIGN_CENTER});
    ui_root->setProperty(KK_CSS_PROPERTY_LEFT, KKUIPropertyValue{100});
    ui_root->setProperty(KK_CSS_PROPERTY_TOP, KKUIPropertyValue{40});
    ui_root->setProperty(KK_CSS_PROPERTY_BACKGROUND_COLOR, KKUIPropertyValue{KKColor{150, 25, 0, 100}});
    win->SetUITree(ui_root);

    // layout test
    ui_root->layout.type = KK_LAYOUT_TYPE_FLEX;
    ui_root->layout.flex_direction_row = true;
    ui_root->layout.flex_content_align_y = KK_TEXT_ALIGN_CENTER;

    // child
    auto lay1 = new KKUIBase(ui_root, 200, 300);
    lay1->setProperty(KK_CSS_PROPERTY_CONTENT, KKUIPropertyValue{"lay1"});
    lay1->setProperty(KK_CSS_PROPERTY_MARGIN, KKUIPropertyValue{KKSpace{0, 0, 10, 0}});
    lay1->setProperty(KK_CSS_PROPERTY_BACKGROUND_COLOR, KKUIPropertyValue{KKColor{255, 0, 0, 255}});
    lay1->layout.flex_grow = 0;

    auto lay2 = new KKUIBase(ui_root, 100, 500);
    lay2->setProperty(KK_CSS_PROPERTY_CONTENT, KKUIPropertyValue{"lay2"});
    lay2->setProperty(KK_CSS_PROPERTY_BACKGROUND_COLOR, KKUIPropertyValue{KKColor{0, 255, 0, 255}});
    lay2->setProperty(KK_CSS_PROPERTY_BORDER_WIDTH, KKUIPropertyValue{KKSpace{4, 0, 0, 2}});
    lay2->setProperty(KK_CSS_PROPERTY_BORDER_COLOR, KKUIPropertyValue{KKSpaceColor{KKColor{0, 0, 255, 255}}});
    lay2->layout.flex_grow = 1;
    
    // add sub in lay2
    {
        auto item1 = new KKUIBase(lay2, 200, 200);
        item1->setProperty(KK_CSS_PROPERTY_CONTENT, KKUIPropertyValue{"item1"});
        item1->setProperty(KK_CSS_PROPERTY_LEFT, KKUIPropertyValue{10});
        item1->setProperty(KK_CSS_PROPERTY_TOP, KKUIPropertyValue{40});
        item1->setProperty(KK_CSS_PROPERTY_BACKGROUND_COLOR, KKUIPropertyValue{KKColor{0, 0, 255, 150}});

        auto item2 = new KKUIBase(lay2, 200, 200);
        item2->setProperty(KK_CSS_PROPERTY_CONTENT, KKUIPropertyValue{"item2"});
        item2->setProperty(KK_CSS_PROPERTY_LEFT, KKUIPropertyValue{150});
        item2->setProperty(KK_CSS_PROPERTY_TOP, KKUIPropertyValue{140});
        item2->setProperty(KK_CSS_PROPERTY_BACKGROUND_COLOR, KKUIPropertyValue{KKColor{255, 0, 0, 150}});
    }
}
int main(int argc, char **argv)
{
    printf("hello world!\r\n");
    auto app = KKApplication::GetInstance();
    // 第一个窗口
    auto win = app->WindowCreate(800, 600, "kkgui 演示程序! 😀", 0);
    win->SetRender(new KKRenderGdiPlus());
    updateUI(win);
    win->Show(true);
    // 第二个，支持多窗口
    // auto win2 = app->WindowCreate(800, 600, "kkgui 演示程序 2", 0);
    // win2->SetRender(new KKRenderGdiPlus());
    // updateUI(win2);
    // win2->Show(true);
    app->Run();
    return 0;
}